from PySide import QtGui
import b_node
import b_position

class EditNodes(QtGui.QWidget):
	
	def __init__(self, parent):
		super(EditNodes, self).__init__()
		self.parent = parent
		self.errormsg = QtGui.QErrorMessage()
		self.initUI()

	def initUI(self):
		self.listnodes = QtGui.QListWidget()
		self.listnodes.addItems(self.parent.data.nodes.keys())
		self.listnodes.sortItems()
		self.listnodes.itemSelectionChanged.connect(self.selectionChanged)
		
		row = 0
		grid = QtGui.QGridLayout()
		name = QtGui.QLabel('Name')
		self.nameedit = QtGui.QLineEdit()
		grid.addWidget(name, row, 0)
		grid.addWidget(self.nameedit, row, 1)
		row += 1
		position = QtGui.QLabel('Position')
		grid.addWidget(position, row, 0)
		self.positionedit = []
		for i in range(0, self.parent.data.settings.dimensions):
			self.positionedit.append(QtGui.QLineEdit())
			self.positionedit[i].setValidator(QtGui.QDoubleValidator())
			grid.addWidget(self.positionedit[i], row, 1)
			row += 1
		
		self.addButton = QtGui.QPushButton("Add Node")
		self.addButton.clicked.connect(self.addNode)
		self.modifyButton = QtGui.QPushButton("Modify Node")
		self.modifyButton.clicked.connect(self.modifyNode)
		self.deleteButton = QtGui.QPushButton("Delete Node")
		self.deleteButton.clicked.connect(self.deleteNode)
		buttons = QtGui.QHBoxLayout()
		buttons.addWidget(self.addButton)
		buttons.addWidget(self.modifyButton)
		buttons.addWidget(self.deleteButton)
		
		editdialog = QtGui.QVBoxLayout()
		editdialog.addLayout(grid)
		editdialog.addStretch(1)
		editdialog.addLayout(buttons)
		
		hbox = QtGui.QHBoxLayout()
		hbox.addWidget(self.listnodes)
		hbox.addLayout(editdialog)
		
		self.setLayout(hbox)
		self.setWindowTitle("Edit Nodes")
		
	def closeEvent(self, event):
		self.hide()
	
	def modifyNode(self):
		current = self.listnodes.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			for i in range(0, self.parent.data.settings.dimensions):
				self.parent.data.nodes[name].position.x[i] = float(self.positionedit[i].text())
			nameedit = str(self.nameedit.text())
			if nameedit != name:
				if nameedit in self.parent.data.nodes.keys():
					self.errormsg.showMessage("Node by that name already exists")
					return
				self.parent.data.nodes[name].name = nameedit
				self.parent.data.nodes[nameedit] = self.parent.data.nodes[name]
				del self.parent.data.nodes[name]
				self.parent.updateAll(self)
			self.parent.updateNode(self.parent.data.nodes[nameedit])
			for member in self.parent.data.members.values():
				for node in member.nodes:
					if node is self.parent.data.nodes[nameedit]:
						self.parent.updateMember(member)
						break
		else:
			self.errormsg.showMessage("No node selected to modify")
	
	def addNode(self):
		x = []
		for i in range(0, self.parent.data.settings.dimensions):
			x.append(float(self.positionedit[i].text()))
		tempposition = b_position.Position(self.parent.data, x)
		node = b_node.Node(self.parent.data, self.nameedit.text(), tempposition)
		if node.name in self.parent.data.nodes:
			self.errormsg.showMessage("Node by that name already exists, cannot add")
		else:
			self.parent.data.nodes[node.name] = node
			self.parent.layout.view.addNode(node)
			self.parent.updateAll(self)
	
	def deleteNode(self):
		current = self.listnodes.currentItem()
		if current is not None and current.isSelected():
			name = str(current.text())
			node = self.parent.data.nodes[name]
			for member in self.parent.data.members.values():
				for nodetemp in member.nodes:
					if nodetemp is node:
						member.deleteMember()
						self.parent.layout.view.deleteMember(member)
						break
			node.deleteNode()
			self.parent.layout.view.deleteNode(node)
			self.parent.updateAll(self)
	
	def selectionChanged(self):
		current = self.listnodes.currentItem()
		if current is not None and current.isSelected():
			name = current.text()
			self.nameedit.setText(name)
			for i in range(0, self.parent.data.settings.dimensions):
				self.positionedit[i].setText(str(self.parent.data.nodes[name].position.x[i]))
	
	def updateNodes(self, check=None):
		if check is self:
			self.updateList()
	
	def updateList(self):
		self.listnodes.itemSelectionChanged.disconnect(self.selectionChanged)
		self.listnodes.clear()
		self.listnodes.addItems(self.parent.data.nodes.keys())
		self.listnodes.sortItems()
			self.listnodes.itemSelectionChanged.connect(self.selectionChanged)
